<template>
    <div>
        <h2>修改积分页面</h2>
        <table class="table table-bordered">
            <tr>
            <td>积分</td>
            <td><input type="text" v-model="state.intergral"></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="增加" @click="comlc(num)">
                    <input type="button" value="减少" @click="comlc(-num)">
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="保存" @click="save">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios  from 'axios';
import { reactive,toRefs,onMounted } from 'vue';
import { useRouter,useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
const data = reactive({
    state:{},
    integral:'',
    num:100
    
})

const {state,integral,num} = toRefs(data);

//反填
onMounted(()=>{
    fantian()
})

const fantian = ()=>{
    axios({
        url:'/api/Member/FantianId',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then(res => {
        console.log(res);
        state.value= res.data.data;
        integral.value=res.data.data.integral
    })
    .catch(err => {
        console.log(err);
    })
}


const comlc = (num:any)=>{
    if(state.value.integral<=0&&num<0)
    {
        return;
    }
    state.value.integral=Number(state.value.integral)+num;
}

const save = ()=>{
    if(state.value.integral=='')
    {
        alert('积分不能为空');
        return;
    }
    if(isNaN(Number(state.value.integral)))
    {
        alert('积分必须填数字类型');
        return;
    }
    let obj={
        id:route.query.id,
        integral:state.value.intergral
    }
    axios({
        url:'/api/Member/UpdIntergral',
        method:'put',
        params:obj
    })
    .then(res => {
        console.log(res);
        if(res.data.data>0)
        {
            alert('修改积分成功');
            router.push('/ShowMember');
        }
        else{
            alert('修改积分失败');
            return;
        }
    })
    .catch(err => {
        console.log(err);
    })
}
</script>

<style scoped>

</style>